const axios = require('axios');
const cheerio = require('cheerio');
const fs = require('fs');
const ExcelJS = require('exceljs');

// 目标网页的 URL
const url = 'https://blog.csdn.net/Tingfeng__/article/details/141380750?spm=1001.2100.3001.7377&utm_medium=distribute.pc_feed_blog_category.none-task-blog-classify_tag-1-141380750-null-null.nonecase&depth_1-utm_source=distribute.pc_feed_blog_category.none-task-blog-classify_tag-1-141380750-null-null.nonecase'; // 替换为你要抓取的网页 URL

// 创建 Excel 工作簿和工作表
const workbook = new ExcelJS.Workbook();
const worksheet = workbook.addWorksheet('Articles');

// 设置表头
worksheet.columns = [
    { header: '标题', key: 'title', width: 50 },
    { header: '链接', key: 'link', width: 50 }
];

const baseUrl = 'https://example.com/articles';
const startPage = 1;
const endPage = 5;
const data = [];

// 抓取单页数据并保存到数组
async function fetchPage(page) {
    try {
        const { data: pageData } = await axios.get(`${baseUrl}?page=${page}`);
        const $ = cheerio.load(pageData);

        $('.article').each((index, element) => {
            const title = $(element).find('.article-title').text();
            const link = $(element).find('a').attr('href');
            data.push({ title, link });
        });
    } catch (error) {
        console.error(`抓取第 ${page} 页失败: ${error.message}`);
    }
}

// 批量抓取并保存到 Excel
async function fetchPages() {
    const pages = [];
    for (let i = startPage; i <= endPage; i++) {
        pages.push(fetchPage(i));
    }
    await Promise.all(pages);

    // 将数据转换为工作表
    const ws = XLSX.utils.json_to_sheet(data);
    const wb = XLSX.utils.book_new();
    XLSX.utils.book_append_sheet(wb, ws, 'Articles');

    // 保存 Excel 文件
    XLSX.writeFile(wb, 'articles.xlsx');
    console.log('数据已保存到 articles.xlsx');
}

// 启动抓取和保存
fetchPages();




